<?php

session_start();

if (!$_SESSION['name']){
	header('Location: login.php');
	die;
}

$name = $_SESSION['name'];
$user_id = $_SESSION['user_id'];
$user_data = array();

$caidan = array();
require_once 'caidan.php';
include './source/class/class_core.php';

	
$date_time = strtotime(date('Y-m-d'));
$user_order = DB::fetch_first("SELECT * FROM `om_order` WHERE order_index = '$date_time' AND name = '$name'");
if ( $user_order ){
	$user_data = unserialize($user_order['order_info']);
	
	
	//echo '<pre>';
	//print_r($user_data);
	//print_r(array_keys($user_data['num']));
	//print_r($caidan);
	//die;
	
	//print_r(unserialize($user_order['order_info_ext']));
	
}

// 订单锁功能
$lock = file_get_contents('lock.php');

$user_info = DB::fetch_first("SELECT * FROM `om_user` WHERE id = $user_id");


?>
<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>下单</title>
	<link rel="stylesheet" href="images/global.min.css">
<script>
function subform(){
	var num = document.getElementsByName("num[]");
	
	for(i=0,j=num.length;i<j;i++){
		if(parseInt(num[i].value) != 0){
			return true;
		}
	}
	alert("提示：你没有选定任何菜式！");
	return false;
}
</script>
</head>
<body id="index">
<form action="action_order.php" method="post" onsubmit="return subform();">
<input type="hidden" name="action" value="save" />
    <div id="header">
        <strong class="fl">你好：<?=$name ?> 账户余额：<?=$user_info['money'] ?></strong>
        <?php if (empty($user_data)){?>
        <p>你今天还没下单，欢迎订餐！</p>
        <div class="fr">
        <a href="history.php">查看总订单</a>
		<a href="user_center.php">账户中心</a>
        <a href="login.php?loginout">退出登录</a>
        </div>
        <?php }?>
        <?php if (!empty($user_data)){?>
        <p>你已经下过单(已高亮)，你可以选择取消订单或重新下单(重新下单请把不需要的饭改为0份，以免重复！)</p>
        <div class="fr">

<?php 
// 锁单功能，第一个用户为超级用户，过滤锁定功能 1锁，0不锁
if($lock == 1 && $name != 'MM'){
	echo "<span>订单已被管理员锁定，不能取消订单！</span>";
} else {
	echo "<a href='action_order.php?action=cancle'>取消订单</a>";
}
?>


        
        <a href="history.php">查看总订单</a>
		<a href="user_center.php">账户中心</a>
        <a href="login.php?loginout">退出登录</a>

        </div>
        <?php }?>
 		<?php
			if($lock == 1 && $name == 'MM'){
				echo "<a href='action_lock.php?action=lock&value=0'> 解锁 </a>";
			} elseif($lock == 0 && $name == 'MM'){
				echo "<a href='action_lock.php?action=lock&value=1'> 加锁 </a>";
			}
		?>
    </div>
<ul class="clearfix">
		<?php foreach ($caidan as $key => $cai){?>
		<li<?=$user_data['num'][$key] != 0 ?" class='highlight'":'';?>>
			<span class="name"><?=$cai['name'] ?> (￥<?=$cai['price'] ?>)</span>
			<select name="num[]">
				<option value="0" <?=$user_data['num'][$key] == 0 ?'selected':'' ?>>0份</option>
				<option value="1" <?=$user_data['num'][$key] == 1 ?'selected':'' ?>>1份</option>
				<option value="2" <?=$user_data['num'][$key] == 2 ?'selected':'' ?>>2份</option>
				<option value="3" <?=$user_data['num'][$key] == 3 ?'selected':'' ?>>3份</option>
				<option value="4" <?=$user_data['num'][$key] == 4 ?'selected':'' ?>>4份</option>
				<option value="5" <?=$user_data['num'][$key] == 5 ?'selected':'' ?>>5份</option>
			</select>
			<select name="la[]">
				<option value="0" <?=$user_data['la'][$key] == 0 ?'selected':'' ?>>不辣</option>
				<option value="1" <?=$user_data['la'][$key] == 1 ?'selected':'' ?>>微辣</option>
				<option value="2" <?=$user_data['la'][$key] == 2 ?'selected':'' ?>>中辣</option>
				<option value="3" <?=$user_data['la'][$key] == 3 ?'selected':'' ?>>特辣</option>
			</select>
		</li>
		<?php }?>
</ul>
<div class="remark">
<div>备注：（如：加白饭, 多糖少奶）</div>
<div<?=$user_data['remark'] != '' ?" class='highlight'":'';?>><textarea rows="" cols="40" name="remark"><?=$user_data['remark'] ?></textarea></div>
</div>

<?php 
// 锁单功能，第一个用户为超级用户，过滤锁定功能 1锁，0不锁
if($lock == 1 && $name != 'MM'){
	echo "<button name='submit' onclick='return false'> 亲！管理员已经锁定今天的订单，请联系管理员追加订餐！ </button>";
} else {
	echo "<button type='submit' name='submit'> 下 单 </button>";
}
?>

</form>